package com.sunnyatwal.kettlebell.sounds;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;


public class BeepIDManager {

	private int mBeepIdComplete;
	private int mBeepIdNext;
	private int mBeepIdCountDown;
	
	private SoundPool mSoundPool;
	
	private boolean mSoundPoolLoaded = false;
	
		
	public BeepIDManager(Context context, int countDown, int next, int complete) {
				
		mSoundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
		
		mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
			
		      @Override
		      public void onLoadComplete(SoundPool soundPool, int sampleId,
		          int status) {
		    	  mSoundPoolLoaded = true;
		      }
		      });
		
		mBeepIdCountDown = mSoundPool.load(context, countDown, 1);
		mBeepIdNext = mSoundPool.load(context, next, 1);
		mBeepIdComplete = mSoundPool.load(context, complete, 1);
	}
	
	
	public int getBeepID(BeepID id) {
		
		switch(id) {
		
		case COUNTDOWN:
			return mBeepIdCountDown;
			
		case NEXT:
			return mBeepIdNext;
			
		case COMPLETE:
			return mBeepIdComplete;
			
		default:
			return 0;
		}
	}
	
	public void playBeep(BeepID beep) throws Exception {
		
		if(!mSoundPoolLoaded) {
			throw new Exception("Sound pool not loaded");
		}
		
		int requestedBeepId = getBeepID(beep);
				
		if (requestedBeepId < 1) {
			throw new Exception("Can't find sound resource");
		}
		
      	mSoundPool.play(requestedBeepId, 1f, 1f, 1, 0, 1f);		
	}
	
	
}
